package com.baidumap.administrator.happystudy.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.ui.ModifyAddressActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@SuppressWarnings("all")
public class MyAddressAdapter extends BaseAdapter {
	private List<Map<String, Object>> lists;
	private Context context;
	private ViewHolder holder;
	private LayoutInflater inflater;
	private ImageLoader loader;
	private DisplayImageOptions options;
	private ArrayList<A> list;

	public MyAddressAdapter(Context _Context, ImageLoader loader) {

		this.context = _Context;
		this.loader = loader;

	}

	public void bindData(List<Map<String, Object>> list) {
		this.lists = list;
		initDate1();
	}

	private void initDate1() {
		list = new ArrayList<A>();
		A a;
		//初始化哪个地址被选中
		for (int i = 0; i < lists.size(); i++) {
//			if ("1".equals(lists.get(i).get("is_default").toString())) {
//				a = new A(true);
//			} else {
				a = new A(false);
//			}
			list.add(a);
		}
	}

	private void closeDate() {
		list = new ArrayList<A>();
		A a;
		for (int i = 0; i < lists.size(); i++) {
			a = new A(false);
			list.add(a);
		}
	}

	class ViewHolder {
		LinearLayout mAddressCheckRlay;
		ImageView mAddressCheckImg;
		TextView mNameTv;
		TextView mAddressTv;
		LinearLayout modify_lay;
		LinearLayout mDelAddressLay;
	}

	@Override
	public int getCount() {
		return lists.size();
	}

	@Override
	public List<Map<String, Object>> getItem(int position) {
		return (List<Map<String, Object>>) lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final int index = position;
		if (convertView != null) {

			holder = (ViewHolder) view.getTag();

		} else {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_address, null);
			holder = new ViewHolder();
			holder.mAddressCheckImg = (ImageView) convertView
					.findViewById(R.id.address_check_img);

			holder.mAddressCheckRlay = (LinearLayout) convertView
					.findViewById(R.id.address_check_rlay);
			holder.mNameTv = (TextView) convertView.findViewById(R.id.name_tv);

			holder.mAddressTv = (TextView) convertView
					.findViewById(R.id.address_tv);

			holder.mDelAddressLay = (LinearLayout) convertView
					.findViewById(R.id.del_address_lay);

			holder.modify_lay = (LinearLayout) convertView
					.findViewById(R.id.modify_lay);
			convertView.setTag(holder);
		}

//		holder.mNameTv.setText(lists.get(position).get("name").toString() + " "
//				+ lists.get(position).get("phone").toString());
//		holder.mAddressTv.setText(lists.get(position).get("address_gps")
//				.toString()
//				+ " " + lists.get(position).get("address_other").toString());
		holder.mAddressCheckRlay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				closeDate();
				list.get(position).type = true;
				//((MyAddressActivity) context).UserAddressDefaultPost(lists
						//.get(index).get("user_address_id").toString());
				notifyDataSetChanged();
			}
		});
		
		holder.mDelAddressLay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				//((MyAddressActivity) context).UserAddressDelPost(lists
						//.get(index).get("user_address_id").toString());
				
			}
		});
		holder.modify_lay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent it = new Intent(context, ModifyAddressActivity.class);
				context.startActivity(it);

			}
		});

		if (list.get(index).type == true) {

			holder.mAddressCheckImg
					.setBackgroundResource(R.mipmap.address_check);
		} else {
			holder.mAddressCheckImg
					.setBackgroundResource(R.mipmap.address_check1);
		}
		return convertView;
	}

	class A {

		public static final int TYPE_CHECKED = 1;
		public static final int TYPE_NOCHECKED = 0;

		boolean type;

		public A(boolean type) {
			this.type = type;
		}
	}

}
